<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>食堂可购买商品</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote">
            食堂可购买商品
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="productName" autocomplete="off" class="layui-input" placeholder="商品名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品类别</label>
                            <div class="layui-input-inline">
                                <!-- <select name="productCategoryName" lay-search>
                                    <option value=""></option>
                                </select>  -->
                                <input type="text" name="categoryId" autocomplete="off" id="tree" lay-filter="tree" class="layui-input">
                                <!-- <input type="text" name="productCategoryName" autocomplete="off" class="layui-input" placeholder="商品类别" maxlength="20"> -->
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary" onclick="javascript:onReset()">重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>

    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="export">导出</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'treeSelect'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            treeSelect = layui.treeSelect;
        var excelList,currPage

        var canteenId = http.getPid('id')

        // 重置
        window.onReset = function() {
            treeSelect.revokeNode('tree', function(d){});
        }
        // 分类查询
        // getCategory()
        // function getCategory() {
        //     http.get('admin/product/category/listAllCategory',{},function(res){
        //         if(res.code == 200){
        //             var html = '<option value="">请选择商品类别</option>';
        //             var data = res.data;
        //             for (var i = 0; i < data.length; i++) {
        //                 html += '<option value="' + data[i].categoryName + '">' + data[i].categoryName + '</option>'
        //             }
        //             $('select[name=productCategoryName]').html(html);
        //             form.render('select');
        //         }else{
        //             layer.msg(res.msg)
        //         }
        //     })
        // }
        getCategory()
        function getCategory() {
            var authentication = localStorage.getItem('xi-can-management-authentication')
            treeSelect.render({
                // 选择器
                elem: '#tree',
                // 数据
                data: TUrl + 'admin/product/category/listAllTree',
                // 异步加载方式：get/post，默认get
                type: 'get',
                headers: {
                    authentication: authentication
                },
                // 占位符
                placeholder: '商品类别',
                // 是否开启搜索功能：true/false，默认false
                search: true,
                style: {
                    folder: {
                        enable: false
                    },
                    line: {
                        enable: true
                    }
                },
                // 点击回调
                click: function(d){
                    // // console.log(d);
                },
                // 加载完成后的回调函数
                success: function (d) {
                    // // console.log(d);
                }
            });
        }

        // 食堂列表
        var selectTable;
        selectTable = http.table({
            elem: '#selectTable',
            url: 'admin/check/list/listPage',
            method: 'get',
            id: 'selectTable',
            toolbar: '#toolbarDemo',
            where: {
                canteenId: canteenId
            },
            done: function(res, curr, count){
			    excelList = res.data;
                currPage = curr
			},
            cols: [[
                {title:'序号',type:'numbers',fixed: 'left'}
                ,{field:'', title: '主图', align : 'center',event: 'imgclick',
                    templet: function (d) {
                        return '<div ><img src="'+(TUrl+d.mainImage)+'" alt="" width="50px" height="30px"></div>'
                    }
                }
                ,{field:'productName', title: '商品名称', align : 'center',
                    templet: function(d){
                        var html = ''
                        html += '<a class="buttoncolor" href="productDetail.html?id='+d.productId+'">'+d.productName+'</a>'
                        return html
                    }
                }
                ,{field:'productCategoryName', title: '商品类别', align : 'center'}
                ,{field:'unitName', title: '单位', align : 'center'}
                ,{field:'price', title: '价格', align : 'center'}
            ]]
        });

        //查询
        form.on('submit(searchBth)', function (data) {
            let _data = data.field,
				param = _data;
			param.canteenId = canteenId;
			selectTable.reload({
				page:{curr:1},
				where: param
			})
        })

        // table操作
        table.on('tool(selectTable)', function(obj){
            var data = obj.data;
            if(obj.event=='imgclick'){
                var img = data.mainImage
                //页面层
                layer.open({
                    type: 1,
                    title:'查看',
                    content: '<div style="text-align:center"><img src="' + TUrl+img + '" style="height:450px"/></div>',
                    area: ['700px', '500px'], //宽高
                    shadeClose: true, //开启遮罩关闭
                    end: function (index, layero) {
                        return false;
                    }
                });
            }
        })

        // table的toolbar
        table.on('toolbar(selectTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data; //获取选中行数据
            if(obj.event=='export'){ // 导出
				var data = excelList; 
				if (data.length == 0) {
					layer.msg('当前页没有可导出的数据');
					return;
                }
                var param = {
                    page: currPage,
                    limit: 10,
                    authentication: localStorage.getItem('xi-can-management-authentication'),
                    // productCategoryName: $('input[name=productCategoryName]').val(),
                    categoryId: $('input[name=categoryId]').val(),
                    productName: $('input[name=productName]').val(),
                    canteenId: canteenId
                }
                if (param) {  
                    let paramsArray = [];  
                    //encodeURIComponent  
                    Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                    window.open(TUrl+"admin/check/list/export?"+paramsArray.join('&'))
                }
                
			}
        })

    })

</script>
</html>